<div class="row-fluid">
	<div class="span12">
		<ul class="breadcrumb">
			<li>
				<a href="index.php">
					<i class="radmin-icon radmin-home"></i>
					<?php echo t('page', 'Dashboard'); ?>
				</a>
				<span class="divider">/</span>
			</li>
			<li class="active">
				<i class="radmin-icon radmin-clipboard-2"></i>
				<?php echo t('page', 'WHM Accounts'); ?>
			</li>
		</ul>
	</div>
</div>

<div class="row-fluid">
	<div class="span9">
		<h4 class="title"><?php $this->widget('application.components.mywidgets.WikiHelp', array('wiki' => 'whm_accounts')); ?> <?php echo t('page', 'WHM Accounts'); ?></h4>
		<div class="squiggly-border"></div>
		<div class="row-fluid">
			<div class="span12 hide" id="account_list_success_info">
				<div class="alert alter-block alert-success">
		<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
					<h4>Success</h4>
					<div class="content"></div>
				</div>
			</div>
		</div>
		
		<div class="row-fluid">
			<div class="span12 hide" id="account_list_error_info">
				<div class="alert alter-block alert-error">
		<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
					<h4>Error</h4>
					<div class="content"></div>
				</div>
			</div>
		</div>
		
		<?php $form = $this->beginWidget('CActiveForm', array(
			'id' => 'account-list-form',
			'method' => 'get'
		)); ?>
			<?php $this->widget('zii.widgets.grid.CGridView', array(
				'id' => 'account_grid',
				'summaryText' => '',
				'dataProvider' => $whmaccount->search(),
				'columns' => array(
					array(
						'header' => 'Id',
						'value' => '$data->id_account',
						'headerHtmlOptions' => array('class' => 'hide'),
						'htmlOptions' => array('class' => 'hide id_account')
					),
					array(
						'header' => '',
						'type' => 'raw',
						'value' => 'CHtml::checkBox("accounts[checked][]", false, array("class" => "accounts_check"))'
					),
					array(
						'header' => t('page', 'Username'),
	                    'value' => '$data->username',
						'htmlOptions' => array('class' => 'username'),
						'headerHtmlOptions' => array('class' => 'username_sort'),
						'name' => 'username_sort',
						'sortable' => true
					),
					array(
						'header' => t('page', 'Domain'),
						'type' => 'raw',
	                    'value' => '$data->domainwithindividuallink',
						'htmlOptions' => array('class' => 'domain'),
						'headerHtmlOptions' => array('class' => 'domain_sort'),
						'name' => 'domain_sort',
						'sortable' => true
					),
					array(
						'header' => t('page', 'IP'),
						'value' => '$data->ipaddress',
						'htmlOptions' => array('class' => 'ipaddress'),
					),
					array(
						'header' => t('page', 'Group'),
						'value' => 'empty($data->Group)? "" : $data->Group->name',
						'headerHtmlOptions' => array('class' => 'group_sort hidden-phone'),
						'htmlOptions' => array('class' => 'group hidden-phone'),
						'name' => 'group_sort',
						'sortable' => true
					),
					array(
						'header' => t('page', 'Host'),
						'type' => 'raw',
						'value' => 'empty($data->Host)? "" : $data->Host->nameWithLoginLink',
						'headerHtmlOptions' => array('class' => 'host_sort hidden-phone'),
						'htmlOptions' => array('class' => 'host hidden-phone'),
						'name' => 'host_sort',
						'sortable' => true
					),
					array(
						'header' => t('page', 'Action'),
						'type' => 'raw',
						'value' => '$data->renderListActionButtons()'
					)
				),
				'itemsCssClass' => 'table table-index table-striped',
				'pagerCssClass' => 'pagination pagination-right',
				'pager' => array(
					'class' => 'LinkPager',
				),
				'htmlOptions' => array(
					'class' => ''
				)
			));?>
		
			<span class="pull-left">
				<input id="accounts_check_all" type="checkbox" />
				<script>
					$('#accounts_check_all').click(function(){
						$('.accounts_check').removeAttr('checked');
						if($(this).is(":checked")){
							$('.accounts_check').attr('checked', true);
						}
					});
	
					$('.accounts_check').live('click', function(){
						$('#accounts_check_all').attr('checked', true);
						$('.accounts_check').each(function(k, v){
							if(!$(v).is(":checked")){
							$('#accounts_check_all').removeAttr('checked');
								return true;
							}
						});
					});
				</script>
				
				<?php echo CHtml::dropDownList('accounts[event]', '', array(/*'toolbox' => 'WHM Toolbox',*/ 'reboot' => 'Reboot Server', 'restart' => 'Restart Services')); ?>
				<?php echo CHtml::tag('button', array('class' => 'btn', 'id' => 'event_go', 'type' => 'submit'), t('buttons', 'Go')); ?>
				<script>
					$('#event_go').click(function(){
						$('.accounts_check').each(function(k, v){
							if($(v).is(":checked")){
								var id_account = $(v).parents('tr').find('.id_account').text();
								$(v).val(id_account);
							}
						});

						var event = $('#accounts_event').val();
						if(event == 'reboot'){
							$('#account-list-form').attr('action', <?php echo json_encode(url('whm/account/reboot')); ?>);
						}else if(event == 'restart'){
							$('#account-list-form').attr('action', <?php echo json_encode(url('whm/account/restart')); ?>);
						}else{
							$('#account-list-form').attr('action', <?php echo json_encode(url('whm/account/toolbox')); ?>);
						}
					});
				</script>
			</span>
			<script>
				$('#account_grid .pagination').before($('.pull-left'));
				$('.pull-left').after('<br /><br />');
				var sort = <?php echo json_encode($sort); ?>;
				sort = sort.split('.');
				if(sort[1] == 'desc'){
					var arrow = $('<i class="icon-arrow-down hidden-phone"></i>');
				}else{
					var arrow = $('<i class="icon-arrow-up hidden-phone"></i>');
				}
				$('#account_grid th.'+sort[0]).append(arrow);
			</script>
		<?php $this->endWidget(); ?>
	</div>
	
	<?php $this->renderPartial('forms/_add', array('whmaccountAddForm' => $whmaccountAddForm, 'whmaccount' => $whmaccount)); ?>
	<?php $this->renderPartial('forms/_edit', array('whmaccountEditForm' => $whmaccountEditForm)); ?>

	<script>
		$(document).ready(function(){
			$('#account_grid .account_edit').live('click', function(){
				var id_account = $(this).parents('tr').find('.id_account').text();
				$('.account_edit-line').removeClass('account_edit-line');
				$(this).parents('tr').addClass('account_edit-line');
				$.ajax({
					url: <?php echo json_encode(url('whm/account/detail')); ?>,
					type: 'get',
					data: {id_account: id_account},
					success: function(detail){
						$('#account-edit-form .fields').val('');
						$.each(detail, function(key, value){
							if($('#account-edit-form #WhmAccountEditForm_'+key)){
								$('#account-edit-form #WhmAccountEditForm_'+key).val(value);
							}
						});
						$('#account-edit-form #WhmAccountEditForm_accesshash').val('');
						$('#account-edit-form #WhmAccountEditForm_pass').val('');
						
						$('#account-add-block').fadeOut(function(){
							$('#account-edit-block').fadeIn();
						});
					},
					dataType: 'json'
				});
				
			});
			
			$('#account_grid .account_delete').live('click', function(){
				$(this).parents('.btn-group').find('.actions').hide();
				$(this).parents('.btn-group').find('.delete_confirm_btn').show();
			});
			$('#account_grid .delete_cancel').live('click', function(){
				$(this).parents('.btn-group').find('.actions').show();
				$(this).parents('.btn-group').find('.delete_confirm_btn').hide();
			});
			$('#account_grid .delete_confirm').live('click', function(){
				var obj = $(this);
				$(this).parents('.btn-group').find('button.actions').attr('data-loading-text', 'deleting').button('loading');
				var id_account = $(this).parents('tr').find('.id_account').text();
				$.ajax({
					url: '<?php echo url('whm/account/delete'); ?>',
					type: 'post',
					data: {id_account: id_account},
					success: function(ret){
						obj.parents('.btn-group').find('button.actions').button('reset');
						if(ret.result){
							obj.parents('tr').remove();
						}else{
							$("#account_list_error_info").blink({
								"beforeCall": function(){
									$("#account_list_error_info .content").html("");
									$.each(ret["errors"], function(attrName, error){
										$(error).each(function(k, e){
											var p = $("<p />").addClass("text-error").append(attrName+": "+e);
											$("#account_list_error_info .content").append(p);
										});
									});
								}
							});
						}
						obj.parents('.btn-group').find('.delete_cancel').click();
					},
					dataType: 'json'
				});
			});

			$('#account_grid .account_pull_cpanel').live('click', function(){
				var obj = $(this);
				$(this).parents('.btn-group').find('button.actions').attr('data-loading-text', 'Refreshing...').button('loading');
				var id_account = $(this).parents('tr').find('.id_account').text();
				$.ajax({
					url: '<?php echo url('whm/account/reload'); ?>',
					type: 'post',
					data: {id_account: id_account},
					success: function(ret){
						obj.parents('.btn-group').find('button.actions').button('reset');
						if(ret.result){
							$("#account_list_success_info").blink({
								"beforeCall": function(){
									$("#account_list_success_info .content").text('Refresh Accounts Data Done!');
								}
							});
						}else{
							$("#account_list_error_info").blink({
								"beforeCall": function(){
									$("#account_list_error_info .content").html("");
									$.each(ret["errors"], function(attrName, error){
										$(error).each(function(k, e){
											var p = $("<p />").addClass("text-error").append(attrName+": "+e);
											$("#account_list_error_info .content").append(p);
										});
									});
								}
							});
						}
						obj.parents('.btn-group').find('.delete_cancel').click();
					},
					dataType: 'json'
				});
			});

			$('#account_grid .account_cpanel_accounts').live('click', function(){
				if($(this).children('a').attr('href') == 'javascript:void(0);'){
					var id_account = $(this).parents('tr').find('.id_account').text();
					window.location.href = '<?php echo url('whm/cpanelaccount/index'); ?>/id_account/'+id_account;
				}

				return true;
			});

			$('#account_grid .account_whm_login').live('click', function(){
				//if($(this).children('a').attr('href') == 'javascript:void(0);'){
					var id_account = $(this).parents('tr').find('.id_account').text();
					$(this).children('a').attr('href', '<?php echo url('whm/account/login'); ?>/id_account/'+id_account);
				//}

				return true;
			});

			$('#account_grid .account_setup_hostlogin').live('click', function(){
				if($(this).children('a').attr('href') == 'javascript:void(0);'){
					var id_account = $(this).parents('tr').find('.id_account').text();
					window.location.href = '<?php echo url('whm/host/logindetail'); ?>/id_account/'+id_account;
				}

				return true;
			});

			$('#switch-account-add-btn').click(function(){
				$('#account-edit-block').fadeOut(function(){
					$('#account-add-block').fadeIn();
				});
			});
		});
	</script>
</div>